<?php

namespace Blog\Controller;

use Blog\Lib\Controller\View;

/**
 * Class Index
 * @property \Blog\Model\Category categoryModel
 * @property \Blog\Model\Blog blogModel
 * @property \Blog\Model\User userModel
 * @property \Blog\Model\Option optionModel
 * @package Blog\Controller
 */
class Index extends View {


    protected function side() {
        $this->view->assign('sideCategories', $this->categoryModel->getRank())
            ->assign('sideHot', $this->blogModel->getHot($this->options['blog']['hotListNumber']));
    }

    public function indexAction() {
        $this->side();
        $this->assignOption();
        $this->view->assign('blogList', $this->blogModel->getList(1, $this->options['blog']['blogListNumber']))
            ->assign('pageUrl', 'page/');
        return $this->view->setTemplate('index');
    }

    public function pageAction($page) {
        $page = (int)$page;
        $this->side();
        $this->assignOption();
        $this->view->assign('blogList', $this->blogModel->getList($page, $this->options['blog']['blogListNumber']))
            ->assign('currentPage', $page)
            ->assign('pageUrl', 'page/');
        $this->assignPageTitle('第 ' . $page . ' 页');
        return $this->view->setTemplate('index');
    }

    public function categoryAction($slug, $page = 1) {
        $category = $this->categoryModel->getBySlug($slug);
        if (!$category) {
            return $this->setError(404);
        }
        $this->side();
        $this->assignOption();
        $this->assignPageTitle($category->name);
        return $this->view->assign('blogList', $this->blogModel->getCategoryList($category->id, $page, $this->options['blog']['blogListNumber']))
            ->assign('currentCategory', $category)
            ->assign('pageUrl', 'category/' . $slug . '/')
            ->setTemplate('index');
    }

    public function userAction($id, $page = 1) {
        $user = $this->userModel->getById($id);
        if (!$user) {
            return $this->setError(404);
        }
        $this->side();
        $this->assignOption();
        $this->assignPageTitle($user->nickName);
        return $this->view->assign('blogList', $this->blogModel->getUserList($id, $page, $this->options['blog']['blogListNumber']))
            ->assign('currentUser', $user)
            ->assign('pageUrl', 'user/' . $id . '/')
            ->setTemplate('index');
    }

    public function allAction() {
        $all = $this->blogModel->getAll(TRUE);
        var_dump($all);
    }

}